package com.amanda.jobs;

import com.amanda.constant.RedisConstant;
import com.amanda.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

/**
 * @author amanda
 * @Description 自定义job，定时清理垃圾图片
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        // 根据redis中保存的两个set集合进行差值计算，获得垃圾图片名称集合
        Set<String> sets = jedisPool.getResource()
                .sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sets != null) {
            for (String imgName : sets) {
                // 在七牛云上删除图片
                QiniuUtils.deleteFileFormQiniu(imgName);
                // 删除redis的图片集合
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, imgName);
                System.out.println(new Date() + " " +"quartz任务：清理七牛云垃圾图片，执行了....");
            }
        }
    }
}
